<?php
/**
 * SHOP 控制台首页
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    exit;
*/
define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');
if($_REQUEST['act'] == 'list'){
    $cat_id=isset($_REQUEST['cat_id'])?$_REQUEST['cat_id']:0;
	$title=isset($_REQUEST['title'])?$_REQUEST['title']:0;
    //查询文章分类列表
    $sql="select list.article_id,list.title,type.cat_name,list.is_open,list.article_type,list.add_time from ".ly()."article list,".ly()."article_cat type where list.cat_id=type.cat_id";
	if($cat_id){
	 $sql.=" and type.cat_id='{$cat_id}'";
	}
	if($title){
		$sql.=" and list.title like '%{$title}%'";
	}
    /*进入列表页*/
	$num='10';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
    $row=$db->getAll($sql); 
    $sql_type="select value from ".ly()."shop_config where code='article_type'";
    $row_type=$db->getOne($sql_type);
    $data=unserialize($row_type);
    foreach ($row as $key => &$value) {
       foreach ($data as $k => $v) {
            if($value['article_type']==$k){
                $value['article_type']=$v;
            }
       }
    }
    //获取文章分类
    $parent_id=$cat_id;
    $sql="select * from ".ly()."article_cat";
    $type=$db->getAll($sql);
    $smarty->assign('type',news_type_cat($parent_id,$type));
    $smarty->assign('list',$row);
    $smarty->display('news_list.html');
}elseif($_REQUEST['act']=='add'){
    $article_id=isset($_REQUEST['article_id'])?$_REQUEST['article_id']:0;
    if($article_id){
         $sql="select * from ".ly()."article where article_id='{$article_id}'";
         $res=$db->getRow($sql);
		 $cat_id=$res['cat_id'];
         $smarty->assign('edit',$res);
    }else{
        $parent_id="";
    }
    $sql="select * from ".ly()."article_cat";
    $type=$db->getAll($sql);
    $smarty->assign('type',news_type_cat($cat_id,$type));
    $smarty->display('news_edit.html');
}elseif($_REQUEST['act']=='edit'){
    $article_id=isset($_POST['article_id'])?$_POST['article_id']:0;
    $title=isset($_POST['title'])?$_POST['title']:0;
    $cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;
    $article_type=isset($_POST['article_type'])?$_POST['article_type']:0;
    $is_open=isset($_POST['is_open'])?$_POST['is_open']:0;
    $author=isset($_POST['author'])?$_POST['author']:0;
    $author_email=isset($_POST['author_email'])?$_POST['author_email']:0;
    $keywords=isset($_POST['keywords'])?$_POST['keywords']:0;
	$content=isset($_POST['content'])?$_POST['content']:0;
    $description=isset($_POST['description'])?$_POST['description']:0;
    $link=isset($_POST['link'])?$_POST['link']:0;
    @$file_url=$_FILES['file_url']['error'];
    if($file_url==0){
        require("../includes/upload_admin.php");//加载图片上传类
        $up=new upphoto;
        $up->get_ph_tmpname($_FILES['file_url']['tmp_name']);
        $up->get_ph_type($_FILES['file_url']['type']);
        $up->get_ph_size($_FILES['file_url']['size']);
        $up->get_ph_name($_FILES['file_url']['name']);
        $up->save();
        $img_url=$up->ph_name;
    }
    if($article_id>0){
        $sql="update ".ly()."article set title='{$title}',cat_id='{$cat_id}',content='{$content}',article_type='{$article_type}',is_open='{$is_open}',author='{$author}',author_email='{$author_email}',keywords='{$keywords}',description='{$description}',link='{$link}',file_url='{$img_url}' where article_id='{$article_id}'";
    }else{
        $sql="insert into ".ly()."article(title,cat_id,content,article_type,is_open,author,author_email,keywords,description,link,file_url) values('{$title}','{$cat_id}','{$content}','{$article_type}','{$is_open}','{$author}','{$author_email}','{$keywords}','{$description}','{$link}','{$img_url}')";
    }
    $db->query($sql);
    if(@mysql_affected_rows){
         $href[]=array('text'=>"返回文章列表",'href'=>'news_list.php?act=list');
         sys_msg("操作成功！",$href);
    }else{
        $href[]=array('text'=>"重新编辑",'href'=>'history.back()');
        sys_msg("操作失败！",$href,fsssssssalse);
    }
}elseif($_REQUEST['act']=='delete'){
        $article_id=isset($_REQUEST['article_id'])?$_REQUEST['article_id']:0;
        //删除文章
        $sql="delete from ".ly()."article where article_id='{$article_id}'";
         $db->query($sql);
         if(@mysql_affected_rows){
            $href[]=array('text'=>"返回文章列表",'href'=>'news_list.php?act=list');
            sys_msg("文章删除成功！",$href);
         }else{
            $href[]=array('text'=>"返回文章列表",'href'=>'news_list.php?act=list');
            sys_msg("文章删除失败！",$href);
         }
}elseif($_REQUEST['act']=='show'){
    //是否显示在导航栏
    $article_id=isset($_POST['article_id'])?$_POST['article_id']:0;
    $show_in_nav=isset($_POST['zt'])?$_POST['zt']:0;
    if($show_in_nav==1){$zt='0';}else{$zt='1';}
    $sql="update ".ly()."article set is_open='{$zt}' where article_id='{$article_id}'";
    $res=$db->query($sql);
    if($res){
        echo "1";   
    }else{
        echo "2";
    }
}
/*新闻分类筛选调用*/
function news_type_cat($p_id=false,$arr){
    $array=array();
    $array=$arr;
    $str="";
    foreach($arr as $v){
        if($v['parent_id']<1){
            if($p_id==$v['cat_id']){$select=" selected='selected'";}else{$select="";}
            $str.= "<option ".$select." value='".$v['cat_id']."'>".$v['cat_name']."</option>";
            $str.= news_type_arr($v['cat_id'],$array,$p_id);
        }
    }
    return $str;
}
/*新闻分类子级调用*/
function news_type_arr($cat_id,$arr,$p_id=false,$nbsp=false){
    $array=array();
    $array=$arr;
    $str="";
    if($nbsp){
        $nbsp.="&nbsp;&nbsp;&nbsp;&nbsp;";
    }else{
        $nbsp="&nbsp;&nbsp;&nbsp;&nbsp;";
    }
    foreach($arr as $v){
        if($cat_id==$v['parent_id']){
            if($p_id==$v['cat_id']){$select=" selected='selected'";}else{$select="";}
            $str.= "<option ".$select." value='".$v['cat_id']."'>".$nbsp.$v['cat_name']."</option>";
            $str.= news_type_arr($v['cat_id'],$array,$p_id,$nbsp);

        }
    }
    return $str;
}

?>